package com.shigu.cron;

import com.opentae.data.mall.pojo.BonusTrade;
import com.opentae.data.mall.interfaces.MallTradeMapper;
import com.opentae.data.mall.interfaces.VipBonusMapper;
import com.shigu.account.api.MallBonusService;
import com.shigu.account.exception.UtilsAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/*
 * create by  过儿  2019-11-11
 * 已完成订单定时给予奖励
 */
@Component
public class DistrubuteMoneyTask {

    @Resource(name="mallBonusServiceImpl")
    MallBonusService mallBonusService;

    @Autowired
    MallTradeMapper mallTradeMapper;

    @Autowired
    VipBonusMapper vipBonusMapper;

    /**
     * @description 定时扫描
     *  先查询相关订单，再通过分配接口进行分润
     * @author 郭尔
     * @date 2019-11-11 15:01
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void distributeTradeBonus(){
        List<BonusTrade> list = mallTradeMapper.selBonusTrade();
        for (BonusTrade b: list) {
            try {
                mallBonusService.doDistributeReward(b.getTradeCode(),b.getRewardId());
            } catch (UtilsAccountException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * @description 处理vip购买
     * @author 郭尔
     * @date 2019-11-12 13:45
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void distributeVipBonus(){
        //每次只处理前100条,防止一次定时太多没跑完
        List<Long> list = vipBonusMapper.waitRecords(100);
        for (Long recordId:list){
            try {
                mallBonusService.doVipBonus(recordId);
            } catch (UtilsAccountException e) {
                e.printStackTrace();
            }
        }
    }

}
